package com.example.llh.zyfypt_110llh.model.follow;

import com.example.llh.zyfypt_110llh.iface.follow.FollowListener;
import com.example.llh.zyfypt_110llh.iface.follow.Followiface;
import com.example.llh.zyfypt_110llh.service.follow.FollowService;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.scalars.ScalarsConverterFactory;

public class FollowModel implements Followiface {
    private Retrofit retrofit;
    private String BASEURL
            ="http://amicool.neusoft.edu.cn/";
    //构造函数
    public FollowModel()
    {   //使用Retrofit----1
        retrofit=new Retrofit.Builder()
                .baseUrl(BASEURL)
                .addConverterFactory(ScalarsConverterFactory.create())
                .build();
    }

    @Override
    public void focus(String mod, int id, String sessionid, final FollowListener listener) {
        //使用Retrofit----2
        FollowService followService=retrofit.create(FollowService.class);
        Call<String> call=followService.focus(mod,id,sessionid);
        //使用Retrofit----3
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                if(response.isSuccessful() && response!=null)
                {  if(response.body().trim().toString().equals("0"))
                    listener.onResponse("1");//关注失败
                else
                    listener.onResponse("2");//关注成功
                }
                else   listener.onFail("collect onresponse fail");//
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                listener.onFail("关注失败1111："+t.toString());
            }
        });
    }

    @Override
    public void unfocus(String mod, int id, String sessionid,final FollowListener listener) {
        //使用Retrofit----2
        FollowService followService=retrofit.create(FollowService.class);
        Call<String> call=followService.unfocus(mod,id,sessionid);
        //使用Retrofit----3
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                if(response.isSuccessful() && response!=null)
                {  if(response.body().trim().toString().equals("0"))
                    listener.onResponse("4");//取消关注失败
                else if (response.body().trim().toString().equals("1"))
                    listener.onResponse("5");//取消关注成功

                }
                else   listener.onFail("uncollect onresponse fail");//uncollect onresponse fail
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                listener.onFail("关注失败："+t.toString());
            }
        });
    }

    @Override
    public void exist(String mod, int id, String sessionid,final FollowListener listener) {
        //使用Retrofit----2
        final FollowService followService=retrofit.create(FollowService.class);
        Call<String> call=followService.exists(mod,id,sessionid);
        //使用Retrofit----3
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                if(response.isSuccessful() && response!=null)
                {  if(response.body().trim().toString().equals("0"))
                    listener.onResponse("7");//已经关注
                else if (response.body().trim().toString().equals("1"))
                    listener.onResponse("8");//未关注
                }
                else   listener.onFail("collect exist onresponse fail");
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                listener.onFail("判断是否关注失败："+t.toString());
            }
        });
    }
}
